/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.explorer.ftypes;

import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import org.json.JSONObject;
import org.webos.java.base.Dialog;
import org.webos.java.base.DialogType;
import org.webos.java.base.NetworkManager;
import org.webos.java.base.RemoteMessage;
import org.webos.java.base.fs.FileDescriptor;
import org.webos.java.base.fs.FileException;
import org.webos.java.base.fs.FileManager;
import org.webos.java.explorer.Explorer;
import org.webos.java.root.Log;

/**
 *
 * @author Kostya
 */

@DialogType(jsClass="explorer.ConnectionDialog")
public class ConnectionDialog extends Dialog{

    private FileDescriptor descriptor = null;
    private Explorer explorer;
    

    public ConnectionDialog(Explorer aExplorer, FileDescriptor aDescriptor) {
        super(aExplorer);
        this.descriptor = aDescriptor;
        explorer = aExplorer;
        if(aDescriptor.getId()!=null){
            try {
                JSONObject object = JSONObject.readJSONObject(aDescriptor.getProvider().getInputStream(aDescriptor));
                setData(object);
                return;
            } catch (Throwable t) {
                Log.error(this, "Exception thrown: " + t.getMessage());
                t.printStackTrace();
            }
        }
        getData().fixString("protocol", "sftp://");
    }

    @RemoteMessage
    public void cancelClick(JSONObject aObject){
        closeDialog(aObject);
    }

    @RemoteMessage
    public JSONObject okClick(JSONObject aObject){
        try {
            descriptor.setName(aObject.optString("name", ""));
            FileManager.checkFileDescriptorName(descriptor);
            if("".equals(aObject.optString("host", "")))
                throw new FileException("Address can't be empty");
            String password = aObject.optString("password", "");
            aObject.putOpt("password", "");
            boolean newFile = descriptor.getId()==null;
            Writer w = new OutputStreamWriter(descriptor.getProvider().getOutputStream(descriptor), "utf-8");
            w.write(aObject.toString());
            w.close();
            String secretID = descriptor.getId()+"-"+aObject.optString("username", "");
            String oldPass = getApplication().getSecret(secretID, "");
            if(!"".equals(password))
            getApplication().setSecret(secretID, password);
            //Try to open URL
            try {
                FileDescriptor[] list = getApplication().getSession().getVFSFileProvider().listFiles(descriptor);
                closeDialog(aObject);
                explorer.fileUpdated(descriptor, newFile, true);
                return null;
            } catch (Throwable t) {
                Log.error(this, "Exception thrown: " + t.getMessage());
                t.printStackTrace();
            }
            //Rollback changes
            try {
                getApplication().setSecret(secretID, oldPass);
                w = new OutputStreamWriter(descriptor.getProvider().getOutputStream(descriptor), "utf-8");
                w.write(getData().toString());
                w.close();
            } catch (Throwable t) {
                Log.error(this, "Exception thrown: " + t.getMessage());
                t.printStackTrace();
            }
            return NetworkManager.getErrorObject(103, "URL verification failed, please check values you provided");
        } catch (Throwable t) {
            if(t instanceof FileException){
                return NetworkManager.getErrorObject(101, t.getMessage());
            }
            Log.error(this, "Exception thrown: " + t.getMessage());
            t.printStackTrace();
            return NetworkManager.getErrorObject(102, "Can't save file");
        }
    }
}
